{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>1、性别</th>\n",
       "      <th>2、年龄</th>\n",
       "      <th>3、户籍</th>\n",
       "      <th>4、受教育程度</th>\n",
       "      <th>5、工作状态</th>\n",
       "      <th>6、和谁一起居住？(独居)</th>\n",
       "      <th>6、(父母)</th>\n",
       "      <th>6、(配偶)</th>\n",
       "      <th>6、(子女)</th>\n",
       "      <th>6、(孙辈)</th>\n",
       "      <th>...</th>\n",
       "      <th>23、我能够更好地帮助亲朋好友</th>\n",
       "      <th>24、网络健康信息质量好坏的评价依据（可多选）(没有标准、凭感觉)</th>\n",
       "      <th>24、(健康信息的具体内容)</th>\n",
       "      <th>24、(健康信息的发布机构)</th>\n",
       "      <th>24、(健康信息的发布日期)</th>\n",
       "      <th>24、(百度结果的排序)</th>\n",
       "      <th>24、(网站自我介绍信息)</th>\n",
       "      <th>躯体健康得分PCS</th>\n",
       "      <th>心理健康得分MCS</th>\n",
       "      <th>31、老年抑郁（GDS-15）得分</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>63</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>44.625632</td>\n",
       "      <td>47.731599</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>75</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>25.634799</td>\n",
       "      <td>50.063603</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>75</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>48.165206</td>\n",
       "      <td>55.253460</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>69</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>45.622986</td>\n",
       "      <td>40.137536</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>69</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>40.892522</td>\n",
       "      <td>38.250543</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 86 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   1、性别  2、年龄  3、户籍  4、受教育程度  5、工作状态  6、和谁一起居住？(独居)  6、(父母)  6、(配偶)  6、(子女)  \\\n",
       "0     2    63     1        2       2              0       0       1       0   \n",
       "1     1    75     2        3       3              1       0       0       0   \n",
       "2     2    75     1        3       2              0       0       0       1   \n",
       "3     1    69     1        3       2              0       0       0       1   \n",
       "4     2    69     2        3       3              0       0       1       0   \n",
       "\n",
       "   6、(孙辈)  ...  23、我能够更好地帮助亲朋好友  24、网络健康信息质量好坏的评价依据（可多选）(没有标准、凭感觉)  \\\n",
       "0       0  ...                3                                  1   \n",
       "1       0  ...                3                                  1   \n",
       "2       0  ...                2                                  0   \n",
       "3       0  ...                4                                  0   \n",
       "4       0  ...                4                                  0   \n",
       "\n",
       "   24、(健康信息的具体内容)  24、(健康信息的发布机构)  24、(健康信息的发布日期)  24、(百度结果的排序)  \\\n",
       "0               1               0               0             0   \n",
       "1               0               0               0             1   \n",
       "2               0               1               0             0   \n",
       "3               1               0               0             0   \n",
       "4               1               1               1             0   \n",
       "\n",
       "   24、(网站自我介绍信息)  躯体健康得分PCS  心理健康得分MCS  31、老年抑郁（GDS-15）得分  \n",
       "0              0  44.625632  47.731599                  2  \n",
       "1              0  25.634799  50.063603                 12  \n",
       "2              0  48.165206  55.253460                  3  \n",
       "3              0  45.622986  40.137536                 10  \n",
       "4              0  40.892522  38.250543                  2  \n",
       "\n",
       "[5 rows x 86 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# Specify the path for the data file and output directory\n",
    "data_path = '../data/output/processed_data.xlsx'  # Change this as needed\n",
    "output_dir = '../output/chapter3/3p4/'  # Change this as needed for your output folder\n",
    "\n",
    "# Load the dataset\n",
    "df = pd.read_excel(data_path)\n",
    "\n",
    "# Display the first few rows of the dataframe\n",
    "df.head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 0: 性别 (Gender, categorical data)\n",
    "gender = df.iloc[:, 0]\n",
    "gender = gender.replace({1: 'Male', 2: 'Female'})\n",
    "\n",
    "# Calculate gender distribution percentages\n",
    "gender_counts = gender.value_counts(normalize=True)\n",
    "\n",
    "# Plot pie chart for gender distribution and save as image\n",
    "gender_pie_chart = output_dir + 'gender_distribution.png'\n",
    "plt.figure(figsize=(8, 8))\n",
    "plt.pie(gender_counts, labels=gender_counts.index, autopct='%1.1f%%', startangle=140)\n",
    "plt.title('Gender Distribution')\n",
    "plt.savefig(gender_pie_chart)\n",
    "plt.close()\n",
    "\n",
    "# Save gender distribution text result\n",
    "gender_text = output_dir + 'gender_distribution.txt'\n",
    "with open(gender_text, 'w', encoding='utf-8') as file:\n",
    "    file.write(f\"Gender Distribution:\\n{gender_counts}\\n\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import seaborn as sns\n",
    "\n",
    "# 1: 年龄 (Age, numerical data)\n",
    "age = df.iloc[:, 1]\n",
    "\n",
    "# Calculate mean, median, and standard deviation\n",
    "age_mean = age.mean()\n",
    "age_median = age.median()\n",
    "age_std = age.std()\n",
    "\n",
    "# Plot histogram for age distribution and save as image\n",
    "age_histogram = output_dir + 'age_distribution.png'\n",
    "plt.figure(figsize=(8, 6))\n",
    "sns.histplot(age, kde=True, bins=20)\n",
    "plt.title(f'Age Distribution (Mean: {age_mean:.2f}, Median: {age_median:.2f}, Std: {age_std:.2f})')\n",
    "plt.xlabel('Age')\n",
    "plt.ylabel('Frequency')\n",
    "plt.savefig(age_histogram)\n",
    "plt.close()\n",
    "\n",
    "# Save age analysis text result\n",
    "age_text = output_dir + 'age_distribution.txt'\n",
    "with open(age_text, 'w', encoding='utf-8') as file:\n",
    "    file.write(f\"Age - Mean: {age_mean:.2f}, Median: {age_median:.2f}, Std: {age_std:.2f}\\n\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2: 户籍 (Hukou, categorical data)\n",
    "hukou = df.iloc[:, 2]\n",
    "hukou = hukou.replace({1: 'Urban', 2: 'Rural'})\n",
    "\n",
    "# Calculate hukou distribution percentages\n",
    "hukou_counts = hukou.value_counts(normalize=True)\n",
    "\n",
    "# Plot pie chart for hukou distribution and save as image\n",
    "hukou_pie_chart = output_dir + 'hukou_distribution.png'\n",
    "plt.figure(figsize=(8, 8))\n",
    "plt.pie(hukou_counts, labels=hukou_counts.index, autopct='%1.1f%%', startangle=140)\n",
    "plt.title('Hukou Distribution')\n",
    "plt.savefig(hukou_pie_chart)\n",
    "plt.close()\n",
    "\n",
    "# Save hukou distribution text result\n",
    "hukou_text = output_dir + 'hukou_distribution.txt'\n",
    "with open(hukou_text, 'w', encoding='utf-8') as file:\n",
    "    file.write(f\"Hukou Distribution:\\n{hukou_counts}\\n\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3: 受教育程度 (Education Level, categorical data)\n",
    "education = df.iloc[:, 3]\n",
    "education = education.replace({\n",
    "    1: 'Primary School', \n",
    "    2: 'Middle School', \n",
    "    3: 'High School', \n",
    "    4: 'Undergraduate', \n",
    "    5: 'Postgraduate1',\n",
    "    6: 'Postgraduate2',\n",
    "    7: 'Postgraduate3',\n",
    "})\n",
    "\n",
    "# Calculate education distribution percentages\n",
    "education_counts = education.value_counts(normalize=True)\n",
    "\n",
    "# Plot pie chart for education distribution and save as image\n",
    "education_pie_chart = output_dir + 'education_distribution.png'\n",
    "plt.figure(figsize=(8, 8))\n",
    "plt.pie(education_counts, labels=education_counts.index, autopct='%1.1f%%', startangle=140)\n",
    "plt.title('Education Level Distribution')\n",
    "plt.savefig(education_pie_chart)\n",
    "plt.close()\n",
    "\n",
    "# Save education level distribution text result\n",
    "education_text = output_dir + 'education_distribution.txt'\n",
    "with open(education_text, 'w', encoding='utf-8') as file:\n",
    "    file.write(f\"Education Level Distribution:\\n{education_counts}\\n\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 4: 工作状态 (Employment Status, categorical data)\n",
    "employment_status = df.iloc[:, 4]\n",
    "employment_status = employment_status.replace({\n",
    "    1: 'Employed', \n",
    "    2: 'Unemployed', \n",
    "    3: 'Retired', \n",
    "  \n",
    "})\n",
    "\n",
    "# Calculate employment status distribution percentages\n",
    "employment_status_counts = employment_status.value_counts(normalize=True)\n",
    "\n",
    "# Plot pie chart for employment status distribution and save as image\n",
    "employment_status_pie_chart = output_dir + 'employment_status_distribution.png'\n",
    "plt.figure(figsize=(8, 8))\n",
    "plt.pie(employment_status_counts, labels=employment_status_counts.index, autopct='%1.1f%%', startangle=140)\n",
    "plt.title('Employment Status Distribution')\n",
    "plt.savefig(employment_status_pie_chart)\n",
    "plt.close()\n",
    "\n",
    "# Save employment status distribution text result\n",
    "employment_status_text = output_dir + 'employment_status_distribution.txt'\n",
    "with open(employment_status_text, 'w', encoding='utf-8') as file:\n",
    "    file.write(f\"Employment Status Distribution:\\n{employment_status_counts}\\n\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "whj",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
